import { getGoodsTypeDataByIdData } from "@/apis/goodsType";
import { useRoute, onBeforeRouteUpdate } from "vue-router";
import { onMounted, ref } from "vue";

export function useGoodsTypeData() {
    const route = useRoute()
    const goodsTypeData = ref(Object)
    const getGoodsTypeDataById = async (id = route.params.id) => {
        const res = await getGoodsTypeDataByIdData(id);
        goodsTypeData.value = res.data;
    }
    onBeforeRouteUpdate((to) => {
        // 存在问题：使用最新的路由参数请求最新的分类数据
        getGoodsTypeDataById(to.params.id)
    })
    onMounted(() => {
        getGoodsTypeDataById();
    })
    return {
        goodsTypeData
    }
}